package core;

import database.CommentDb;
import utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by jimmyhsu on 2016/12/20.
 */
public class StudentAddComment extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rating1 = req.getParameter("rating1rs");
        String rating2 = req.getParameter("rating2rs");
        String rating3 = req.getParameter("rating3rs");
        String courseIdStr = req.getParameter("courseId");
        String type = req.getParameter("type");
        String studentId = (String) req.getSession().getAttribute("username");
        String comment = StringUtils.getUtf8String(req.getParameter("textcomment"));
        resp.setContentType("text/html;charset=gb2312");
        PrintWriter writer = resp.getWriter();
        if (StringUtils.isAnyoneEmpty(rating1, rating2, rating3, comment, courseIdStr, studentId)) {
            writer.print("<script>alert('参数异常');location.href='../Student/comment.jsp';</script>");
        }
        if (type.equals("insert")) {
            CommentDb.insertComment(studentId, Integer.parseInt(courseIdStr), Integer.parseInt(rating1),
                    Integer.parseInt(rating2), Integer.parseInt(rating3), comment);
        } else if (type.equals("update")) {
            CommentDb.updateComment(studentId, Integer.parseInt(courseIdStr), Integer.parseInt(rating1),
                    Integer.parseInt(rating2), Integer.parseInt(rating3), comment);
        }
        writer.print("<script>alert('评论成功');location.href='../Student/comment.jsp';</script>");
    }
}
